<template>
  <el-dialog class="add-dia social-serve-add center-add" :title="$store.state.dia.center.title" :visible.sync="$store.state.dia.center.show"> 
    <el-form>
      <el-form-item v-for="(item, index) in modelData" :key="index" :label="item.name" label-width="130px">
        <el-select class="is-search" v-if="item.select" v-model="form[item.prop]" placeholder="请选择">
          <el-option v-for="item1 in item.children" :key="item1" :label="item1" :value="item1"></el-option>
        </el-select>

        <!-- <el-autocomplete
          class="is-search"
          v-else-if="item.canSearch"
          v-model="form[item.prop]"
          :debounce="600"
          :fetch-suggestions="getSearchData"
          :placeholder="'搜索'+item.name"
          @select="handleSelect"
          @blur="writeAgain(item)"
        >
        </el-autocomplete> -->
        <el-select
          class="is-search"
          v-else-if="item.canSearch"
          v-model="form[item.prop]"
          multiple
          filterable
          remote
          allow-create
          reserve-keyword
          :placeholder="'搜索'+item.name"
          :remote-method="getSearchData">
          <el-option
            v-for="(opt,optIndex) in options"
            :key="optIndex"
            :label="opt.label"
            :value="opt.value">
          </el-option>
        </el-select>
        <el-input v-model="form[item.prop]" v-else auto-complete="off" :placeholder="item.name" @blur="blur(item)"></el-input>

      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$store.state.dia.center.show = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  props:["modelData","tablename"],
  components: {},
  name: "",
  data() {
    return {
      form:{
        sta:"",
        add:""
      },
      options:[],
    };
  },
  computed: {
    getModelData(){
        this.form.add=localStorage.getItem("userName")
        return this.modelData.slice(1)
    }
  },
  methods: {
    save(){
      var url="teacher/addall"
      var params=this.form
      params.tablename=this.tablename
      // console.log(this.form)
      // return
      for(var key in params){
        if(Array.isArray(params[key])){
          var namesArray=[]
          var numbersArray=[]
          var orginalArray=[]
          params[key].forEach(ele1=>{
            if(ele1.toString().indexOf(")")>-1){
              namesArray.push((ele1.toString().slice(0,params[key].toString().indexOf("("))))
              numbersArray.push(/\((.+?)\)/g.exec(ele1.toString())[1])
            }else{
              namesArray.push(ele1)
            }
          })
          params[key+"_data"]=[...params[key]]
          params[key]=namesArray.join(",")
          params[key+"_number"]=numbersArray
        }
      }

      _g.apiPost(url,params).then(res=>{
        _g.toastMsg(res.error?"error":"success",res.msg)
        if(!res.error)this.$store.state.dia.center.show=false
        this.$emit("reflash")
      })
    },

    /**
     * @name 通过标题获取论文详情
     */
    getPaperInfo(item){
      if(!this.form[item.prop])return
      var params={
        key:item.prop,
        value:this.form[item.prop],
        tablename:this.tablename||"outcomes_academic"
      }
      _g.apiPost("teacher/liketitle",params).then(res=>{
        for(var key in res.data){
          if(key.indexOf("author")>-1&&!Array.isArray(res.data[key])&&res.data[key].length>0){
            res.data[key]=res.data[key].split(",")
          }else if(key.indexOf("author")>-1){
            res.data[key]=[]
          }
        }
        this.$set(this,"form",res.data?res.data:this.form)
      })
    },

    blur(item){
       if(item.blur)this.getPaperInfo(item)
     },
     /**
      * @name 远程搜索
      */
     getSearchData(queryString,cb){
       if(queryString)var queryString = queryString.slice(0,queryString.indexOf("("))
       var number = /\((.+?)\)/g.exec(queryString)
       if(number)var queryString = number[1]
      _g.apiPost("teacher/likeauthor",{search:queryString}).then(res=>{
        res.data.forEach(ele=>{ele.label=ele.value=`${ele.name}(${ele.number})`})
        this.$set(this,"options",res.data)
      })
     },
     /**
     * @name 远程搜索选中
     */
     handleSelect(item){
       console.log(this.form)
     },
      loadData() {
          _g.apiPost("teacher/getselect1", {name: this.tablename}).then(res => {
              for (let i = 0; i < this.modelData.length; i++) {
                  if (this.modelData[i].prop && res.data[this.modelData[i].prop]) {
                      this.modelData[i].select = true
                      this.modelData[i].children = res.data[this.modelData[i].prop];
                  }
              }
              this.form.add=localStorage.getItem("userName")
              this.modelData = this.modelData.slice(1)
          })
      }
  },
  created() {
      this.loadData()
  }
};
</script>
<style scoped>
.add {
}
</style>